﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Maticsoft.DBUtility;
using System.Data.OracleClient;
using System.Data;
using DQYT.HTDZ.Model;
using DQYT.HTDZ.IDAL;

namespace DQYT.HTDZ.OracleDAL
{
    public class BDRoleMenuDAL : IBDRoleMenu
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal JSD_ID, decimal MNU_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_sys_role_menu");
            strSql.Append(" where JSD_ID=:JSD_ID and MNU_ID=:MNU_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSD_ID", OracleType.Number,22),
					new OracleParameter(":MNU_ID", OracleType.Number,22)			};
            parameters[0].Value = JSD_ID;
            parameters[1].Value = MNU_ID;

            return DbHelperOra.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BDRoleMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_sys_role_menu(");
            strSql.Append("JSD_ID,MNU_ID,JSE_ID,JSD_ISADD,JSD_ISEDIT,JSD_ISDEL,JSD_ISPRINT,JSD_ISOUTPUT)");
            strSql.Append(" values (");
            strSql.Append(":JSD_ID,:MNU_ID,:JSE_ID,:JSD_ISADD,:JSD_ISEDIT,:JSD_ISDEL,:JSD_ISPRINT,:JSD_ISOUTPUT)");
            OracleParameter[] parameters = {
					new OracleParameter(":JSD_ID", OracleType.Number,4),
					new OracleParameter(":MNU_ID", OracleType.Number,4),
					new OracleParameter(":JSE_ID", OracleType.Number,4),
					new OracleParameter(":JSD_ISADD", OracleType.Number,4),
					new OracleParameter(":JSD_ISEDIT", OracleType.Number,4),
					new OracleParameter(":JSD_ISDEL", OracleType.Number,4),
					new OracleParameter(":JSD_ISPRINT", OracleType.Number,4),
					new OracleParameter(":JSD_ISOUTPUT", OracleType.Number,4)};
            parameters[0].Value = model.JSD_ID;
            parameters[1].Value = model.MNU_ID;
            parameters[2].Value = model.JSE_ID;
            parameters[3].Value = model.JSD_ISADD;
            parameters[4].Value = model.JSD_ISEDIT;
            parameters[5].Value = model.JSD_ISDEL;
            parameters[6].Value = model.JSD_ISPRINT;
            parameters[7].Value = model.JSD_ISOUTPUT;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BDRoleMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_sys_role_menu set ");
            strSql.Append("JSE_ID=:JSE_ID,");
            strSql.Append("JSD_ISADD=:JSD_ISADD,");
            strSql.Append("JSD_ISEDIT=:JSD_ISEDIT,");
            strSql.Append("JSD_ISDEL=:JSD_ISDEL,");
            strSql.Append("JSD_ISPRINT=:JSD_ISPRINT,");
            strSql.Append("JSD_ISOUTPUT=:JSD_ISOUTPUT");
            strSql.Append(" where JSD_ID=:JSD_ID and MNU_ID=:MNU_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Number,4),
					new OracleParameter(":JSD_ISADD", OracleType.Number,4),
					new OracleParameter(":JSD_ISEDIT", OracleType.Number,4),
					new OracleParameter(":JSD_ISDEL", OracleType.Number,4),
					new OracleParameter(":JSD_ISPRINT", OracleType.Number,4),
					new OracleParameter(":JSD_ISOUTPUT", OracleType.Number,4),
					new OracleParameter(":JSD_ID", OracleType.Number,4),
					new OracleParameter(":MNU_ID", OracleType.Number,4)};
            parameters[0].Value = model.JSE_ID;
            parameters[1].Value = model.JSD_ISADD;
            parameters[2].Value = model.JSD_ISEDIT;
            parameters[3].Value = model.JSD_ISDEL;
            parameters[4].Value = model.JSD_ISPRINT;
            parameters[5].Value = model.JSD_ISOUTPUT;
            parameters[6].Value = model.JSD_ID;
            parameters[7].Value = model.MNU_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal JSD_ID, decimal MNU_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_role_menu ");
            strSql.Append(" where JSD_ID=:JSD_ID and MNU_ID=:MNU_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSD_ID", OracleType.Number,22),
					new OracleParameter(":MNU_ID", OracleType.Number,22)			};
            parameters[0].Value = JSD_ID;
            parameters[1].Value = MNU_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BDRoleMenuModel GetModel(decimal JSD_ID, decimal MNU_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select JSD_ID,MNU_ID,JSE_ID,JSD_ISADD,JSD_ISEDIT,JSD_ISDEL,JSD_ISPRINT,JSD_ISOUTPUT from tb_sys_role_menu ");
            strSql.Append(" where JSD_ID=:JSD_ID and MNU_ID=:MNU_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSD_ID", OracleType.Number,22),
					new OracleParameter(":MNU_ID", OracleType.Number,22)			};
            parameters[0].Value = JSD_ID;
            parameters[1].Value = MNU_ID;

            BDRoleMenuModel model = new BDRoleMenuModel();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["JSD_ID"] != null && ds.Tables[0].Rows[0]["JSD_ID"].ToString() != "")
                {
                    model.JSD_ID = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MNU_ID"] != null && ds.Tables[0].Rows[0]["MNU_ID"].ToString() != "")
                {
                    model.MNU_ID = decimal.Parse(ds.Tables[0].Rows[0]["MNU_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSE_ID"] != null && ds.Tables[0].Rows[0]["JSE_ID"].ToString() != "")
                {
                    model.JSE_ID = decimal.Parse(ds.Tables[0].Rows[0]["JSE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSD_ISADD"] != null && ds.Tables[0].Rows[0]["JSD_ISADD"].ToString() != "")
                {
                    model.JSD_ISADD = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ISADD"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSD_ISEDIT"] != null && ds.Tables[0].Rows[0]["JSD_ISEDIT"].ToString() != "")
                {
                    model.JSD_ISEDIT = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ISEDIT"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSD_ISDEL"] != null && ds.Tables[0].Rows[0]["JSD_ISDEL"].ToString() != "")
                {
                    model.JSD_ISDEL = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ISDEL"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSD_ISPRINT"] != null && ds.Tables[0].Rows[0]["JSD_ISPRINT"].ToString() != "")
                {
                    model.JSD_ISPRINT = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ISPRINT"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSD_ISOUTPUT"] != null && ds.Tables[0].Rows[0]["JSD_ISOUTPUT"].ToString() != "")
                {
                    model.JSD_ISOUTPUT = decimal.Parse(ds.Tables[0].Rows[0]["JSD_ISOUTPUT"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM tb_sys_role_menu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_sys_role_menu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.MNU_ID desc");
            }
            strSql.Append(")AS Row, T.*  from tb_sys_role_menu T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOra.Query(strSql.ToString());
        }

        #endregion  Method

        #region 新添加
        /// <summary>
        /// 获得下一个最大值
        /// </summary>
        /// <returns></returns>
        public int GetNextID()
        {
            return DbHelperOra.GetMaxID("JSD_ID", "tb_sys_role_menu");
        }

        /// <summary>
        /// 更新表数据
        /// </summary>
        /// <param name="dt">数据</param>
        /// <param name="JSE_ID">角色ID</param>
        /// <returns></returns>
        public bool Update(DataTable dt, string JSE_ID)
        {
            #region
            string strSQL = "SELECT JSD_ID,MNU_ID,JSE_ID,JSD_ISADD,JSD_ISEDIT,JSD_ISDEL,JSD_ISPRINT,JSD_ISOUTPUT FROM tb_sys_role_menu WHERE jse_id=" + JSE_ID;

            return DbHelperOra.UpdateTableBySql(dt, strSQL);
            #endregion
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public bool Delete(int JSE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_role_menu ");
            strSql.Append(" where JSE_ID=:JSE_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Int32)		};
            parameters[0].Value = JSE_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="alSQL"></param>
        /// <returns></returns>
        public void ExecuteSqlTran(ArrayList alSQL)
        {
            #region
            DbHelperOra.ExecuteSqlTran(alSQL);
            #endregion
        }
        #endregion
    }
}
